import { SMALLINT_PG } from "~/db.pg";

/** 系统中的主要信息-业务入口 */
export enum ENUM_APP {
  /** 异常情况 */
  NONE = -1,

  DROPLET_BUS = 5101,
  DROPLET_TOUCH,

  STREAM_USER = 5201,
  STREAM_ACCESS,
  STREAM_PAY,

  POND_ASSET = 5301,
  POND_MEDIA,
  POND_CONTACT,
  POND_BOOK,
  POND_JOURNAL,

  RIVER_MARKET = 5601,
  RIVER_TON,
  RIVER_STOCK,
  RIVER_VOUCHER,
  RIVER_QUANTIFY,

  /** 系统控制 */
  OCEAN_GATEWAY = 5801,

  /** 数据库 */
  DB_POSTGRESQL_SHANGHAI = 5901,
}

export enum ENUM_APP_PORT_TCP {
  /** 异常情况 */
  NONE = -1,

  DROPLET_BUS = ENUM_APP.DROPLET_BUS + 1000,
  DROPLET_TOUCH = ENUM_APP.DROPLET_TOUCH + 1000,

  STREAM_USER = ENUM_APP.STREAM_USER + 1000,
  STREAM_ACCESS = ENUM_APP.STREAM_ACCESS + 1000,
  STREAM_PAY = ENUM_APP.STREAM_PAY + 1000,

  POND_ASSET = ENUM_APP.POND_ASSET + 1000,
  POND_MEDIA = ENUM_APP.POND_MEDIA + 1000,

  POND_CONTACT = ENUM_APP.POND_CONTACT + 1000,
  POND_BOOK = ENUM_APP.POND_BOOK + 1000,
  POND_JOURNAL = ENUM_APP.POND_JOURNAL + 1000,

  RIVER_MARKET = ENUM_APP.RIVER_MARKET + 1000,
  RIVER_TON = ENUM_APP.RIVER_TON + 1000,
  RIVER_STOCK = ENUM_APP.RIVER_STOCK + 1000,
  RIVER_VOUCHER = ENUM_APP.RIVER_VOUCHER + 1000,
  RIVER_QUANTIFY = ENUM_APP.RIVER_QUANTIFY + 1000,

  OCEAN_GATEWAY = ENUM_APP.OCEAN_GATEWAY + 1000,
}

export enum ENUM_APP_PORT_REST {
  /** 异常情况 */
  NONE = -1,

  DROPLET_BUS = ENUM_APP.DROPLET_BUS + 3000,
  DROPLET_TOUCH = ENUM_APP.DROPLET_TOUCH + 3000,

  STREAM_USER = ENUM_APP.STREAM_USER + 3000,
  STREAM_ACCESS = ENUM_APP.STREAM_ACCESS + 3000,
  STREAM_PAY = ENUM_APP.STREAM_PAY + 3000,

  POND_ASSET = ENUM_APP.POND_ASSET + 3000,
  POND_MEDIA = ENUM_APP.POND_MEDIA + 3000,

  POND_CONTACT = ENUM_APP.POND_CONTACT + 3000,
  POND_BOOK = ENUM_APP.POND_BOOK + 3000,
  POND_JOURNAL = ENUM_APP.POND_JOURNAL + 3000,

  RIVER_MARKET = ENUM_APP.RIVER_MARKET + 3000,
  RIVER_TON = ENUM_APP.RIVER_TON + 3000,
  RIVER_STOCK = ENUM_APP.RIVER_STOCK + 3000,
  RIVER_VOUCHER = ENUM_APP.RIVER_VOUCHER + 3000,
  RIVER_QUANTIFY = ENUM_APP.RIVER_QUANTIFY + 3000,

  OCEAN_GATEWAY = ENUM_APP.OCEAN_GATEWAY + 3000,
}

/** 网关：通过网关来寻找服务 */
export type Gateway = {
  /** 数据表名称 */
  relations: string;

  /** 资源访问路径，它通过数据表名称转换而来 */
  path: string;

  app: ENUM_APP;
  port_tcp: SMALLINT_PG;
  port_rest: SMALLINT_PG;
};

export type GatewayPart = Omit<Gateway, "relations" | "path">;

/** 通过部分网关信息，取得数据表名称、以及对应的资源路径 */
export function getGateway(ports: GatewayPart, relations: string): Gateway {
  return { ...ports, relations, path: `/api/${relations.replace(/_/g, "/")}` };
}
